#ifndef QPAINTERPATHHELPER_H
#define QPAINTERPATHHELPER_H

#include <QPainterPath>
#include <QtMath>

class QPainterPathHelper
{
public:
    explicit QPainterPathHelper(){}

    QVector<QPointF> getInterpolatedPoints(const QPainterPath& path, qreal stepSize = 1.0) {
        QVector<QPointF> points;

        if (path.isEmpty()) {
            return points;
        }

        qreal totalLength = path.length();
        qreal currentLength = 0.0;

        // 按照步长采样点
        while (currentLength <= totalLength) {
            qreal percent = path.percentAtLength(currentLength);
            points.append(path.pointAtPercent(percent));
            currentLength += stepSize;
        }

        // 确保包含终点
        if (!points.isEmpty() && points.last() != path.currentPosition()) {
            points.append(path.currentPosition());
        }

        return points;
    }

    // 计算三次贝塞尔曲线上的点
    QPointF bezierPoint(const QPointF& p0, const QPointF& p1, const QPointF& p2, const QPointF& p3, qreal t) {
        qreal u = 1 - t;
        qreal u2 = u * u;
        qreal u3 = u2 * u;
        qreal t2 = t * t;
        qreal t3 = t2 * t;

        return u3 * p0 + 3 * u2 * t * p1 + 3 * u * t2 * p2 + t3 * p3;
    }


    // 更精确的方法（处理弧形段）
    QVector<QPointF> getPreciseInterpolatedPoints(const QPainterPath& path, qreal stepSize = 1.0) {
        QVector<QPointF> points;

        for (int i = 0; i < path.elementCount(); ++i) {
            const QPainterPath::Element& element = path.elementAt(i);

            switch (element.type) {
            case QPainterPath::MoveToElement:
                points.append(QPointF(element.x, element.y));
                break;

            case QPainterPath::LineToElement: {
                QPointF lastPoint = points.isEmpty() ? QPointF(0, 0) : points.last();
                QPointF lineEnd(element.x, element.y);

                qreal lineLength = QLineF(lastPoint, lineEnd).length();
                int steps = qMax(1, qFloor(lineLength / stepSize));

                for (int j = 1; j <= steps; ++j) {
                    qreal t = qreal(j) / steps;
                    points.append(lastPoint * (1 - t) + lineEnd * t);
                }
                break;
            }

            case QPainterPath::CurveToElement: {
                // 贝塞尔曲线（三次）
                if (i + 2 < path.elementCount()) {
                    QPointF p0 = points.isEmpty() ? QPointF(0, 0) : points.last();
                    QPointF p1(element.x, element.y);
                    QPointF p2(path.elementAt(i+1).x, path.elementAt(i+1).y);
                    QPointF p3(path.elementAt(i+2).x, path.elementAt(i+2).y);
                    i += 2;

                    // 估算曲线长度
                    qreal curveLength = 0.0;
                    QPointF prev = p0;
                    for (qreal t = 0.1; t <= 1.0; t += 0.1) {
                        QPointF pt = bezierPoint(p0, p1, p2, p3, t);
                        curveLength += QLineF(prev, pt).length();
                        prev = pt;
                    }

                    int steps = qMax(1, qFloor(curveLength / stepSize));
                    for (int j = 1; j <= steps; ++j) {
                        qreal t = qreal(j) / steps;
                        points.append(bezierPoint(p0, p1, p2, p3, t));
                    }
                }
                break;
            }

            case QPainterPath::CurveToDataElement:
                // 已在上面的CurveToElement处理
                break;
            }
        }

        return points;
    }

};

#endif // QPAINTERPATHHELPER_H
